<template>
  <view class="scroll-view-container">
    <!-- 左边的一级分类 -->
    <scroll-view class="left-scroll-view" scroll-y>
      <view
        :class="[
          'left-scroll-view-item',
          currentIndex === index ? 'active' : ''
        ]"
        v-for="(item, index) in categories"
        :key="item.cat_id"
        @click="currentIndex = index"
        >{{ item.cat_name }}</view
      >
    </scroll-view>
  </view>
</template>

<script>
export default {
  data () {
    return {
      categories: [], // 分类数据
      currentIndex: 0 // 默认选中的索引
    }
  },
  onLoad () {
    this.getCategoriesData()
  },
  methods: {
    async getCategoriesData () {
      const res = await uni.$request({
        url: 'categories'
      })

      if (res.meta.status === 200) {
        this.categories = res.message
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.scroll-view-container {
  display: flex;

  .left-scroll-view {
    width: 240rpx;

    .left-scroll-view-item {
      line-height: 60px;
      background-color: #f7f7f7;
      text-align: center;
      font-size: 12px;

      // 激活项的样式
      &.active {
        background-color: #ffffff;
        position: relative;

        // 渲染激活项左侧的红色指示边线
        &::before {
          content: ' ';
          display: block;
          width: 3px;
          height: 30px;
          background-color: #c00000;
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
  }

  .right-scroll-view {
    .cate-lv2-title {
      font-size: 12px;
      font-weight: bold;
      text-align: center;
      padding: 15px 0;
    }
    .cate-lv3-list {
      display: flex;
      flex-wrap: wrap;

      .cate-lv3-item {
        width: 33.33%;
        margin-bottom: 10px;
        display: flex;
        flex-direction: column;
        align-items: center;

        image {
          width: 60px;
          height: 60px;
        }

        text {
          font-size: 12px;
        }
      }
    }
  }
}
</style>
